package server;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {
	private static List<ChatServiceHandler> handlers = new ArrayList<ChatServiceHandler>();

	public static void main(String[] args) {
		ServerSocket ss = null;
		try {
			ChatServer server = new ChatServer();// create a ChatServer
			ss = new ServerSocket(8888);
			System.out.println("listener to 8888 ... ...");
			Socket s = null;
			while (true) {
				s = ss.accept(); // 阻塞于此,
				System.out.println("build a link, the ip is :."
						+ s.getInetAddress());
				ChatServiceHandler h = new ChatServiceHandler(s);
				server.handlers.add(h);
				h.setHandlers(server.handlers);
				Thread t = new Thread(h);
				t.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ss.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}
